﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Smarly.Bussines.Model;
using Smarly.Web.Controllers.Base;

namespace Smarly.Web.Areas.Admin.Controllers
{
	public class BookController : AdminBaseController
	{
		[HttpPost]
		public ActionResult CreateBook(Book book)
		{
			if (ModelState.IsValid)
				book.Post();

			TriggerSectionPublished();

			return JsonValidationErrors();
		}

		[HttpPost]
		public ActionResult CreatePart(string current, string after, BookTree node)
		{
			if (node != null && node.Url != null)
			{
				node.Url = node.Url.Trim().ToLower().Replace("&", string.Empty).Replace("?", string.Empty).Replace("'", string.Empty).Replace("’", string.Empty).Replace("/", " ").Replace(":", string.Empty).Replace("  ", " ").Replace(" ", "-").Replace(".", "-");
			}

			if (ModelState.IsValid)
				node.SaveHead(current, after);
			TriggerSectionPublished();
			return JsonValidationErrors();
		}

		[HttpDelete]
		public ActionResult DeletePart(string currentUrl, BookTree node)
		{
			node.Delete(currentUrl);
			TriggerSectionPublished();
			return JsonValidationErrors();
		}

		[HttpPut]
		public ActionResult ChangePublishStatus(string currentUrl)
		{
			BookTree tree = new BookTree().ChangePublishStatus(currentUrl);
			TriggerSectionPublished();
			return JsonValidationErrors();
		}

/*
		[HttpPost]
		public ActionResult EditPart(BookTree node)
		{
			if (ModelState.IsValid)
				node.UpdateHead();
			return JsonValidationErrors();
		}
*/

	}
}
